<?php
include("check.php");
include("check2.php");
include_once("../function.php");
include_once("../class/bonus_class.php");
include_once("../class/ulevel_class.php");
include_once("../class/member_class.php");
include_once("../class/system_class.php");
header("Content-Type: text/html;charset=utf-8");
session_start();
$us=getMemberbyID($_SESSION['ID']);
$_bonus_cl=new bonus_class();
$_ulevel_cl=new ulevel_class();
if ($_POST['submit']){
    $member_cl=new member_class();
    if($us['fenghao']==1){
        echo "<script language=javascript>alert('你已被封号,不能进行此操作.');window.location.href='?'</script>";
    }else{
        $chuju=ischuju($us['id']);
        $fenghao=ismember2($us['id']);
        if($us['futou']==4&&empty($fenghao)){
            echo "<script language=javascript>alert('你已出局,不能进行此操作.');window.location.href='?'</script>";
        }elseif($us['futou']==5){
            echo "<script language=javascript>alert('你已出局,不能进行此操作.');window.location.href='?'</script>";
        }else{
            if($us['futou']==1){
                $tables='member';
                $usmember=getMemberbyID($us['id']);
            }elseif($us['futou']==2){
                $tables='member2';
                $usmember=getMembersbyID($us['id'],$tables);
            }elseif($us['futou']==3){
                $tables='member3';
                $usmember=getMembersbyID($us['id'],$tables);
            }elseif($us['futou']==4){
                $tables='member4';
                $usmember=getMembersbyID($us['id'],$tables);
            }
            $sql="select * from ".$tables." where pid=".($usmember['pid']+6);
            $iscunzai=getOne($sql);
            if($usmember['recount']>=2&&$iscunzai){
                $_iul=$_ulevel_cl->getulevelbyulevel($us['ulevel']);
                if($us['mey']+$us['wlf']>=$_iul['yl5']){
                    if($us['wlf']>=$_iul['yl5']){
                        $member_update['wlf']=$us['wlf']-$_iul['yl5'];
                    }else{
                        $cha=$_iul['yl5']-$us['wlf'];
                        if($cha>0){
                            $member_update['mey']=$us['mey']-$cha;
                            $member_update['wlf']=0;
                        }
                    }
                    edit_update_cl('member',$member_update,$us['id']);
                    if($us['futou']==1){
                        $table='member2';
                    }elseif($us['futou']==2){
                        $table='member3';
                    }elseif($us['futou']==3){
                        $table='member4';
                    }elseif($us['futou']==4){
                        $table='member5';
                    }

                    $sql="select * from ".$table." where ispay>0 order by  pid desc limit 0,1";
                    $array=getOne($sql);
                    if(empty($array)){
                        $member_update['FatherID']=0;
                        $member_update['plevel']=0;
                        $member_update['ppath']="0,";
                        $member_update['area1']=0;
                        $member_update['TreePlace']=1;
                        $member_update['pid']=0;
                        $member_update['userid']=$us['id'];
                        $member_update['ispay']=1;
                        $member_update['nickname']=$us['nickname'];
                        $member_update['ulevel']=$us['ulevel']+1;
                        add_insert_cl("$table",$member_update);
                    }else{
                        $member_update['FatherID']=$array['id'];
                        $member_update['plevel']=$array['plevel']+1;
                        $member_update['ppath']="".$array['ppath'].$array['id'].",";
                        $member_update['area1']=$array['area1'];
                        $member_update['TreePlace']=1;
                        $member_update['futou']=2;
                        $member_update['pid']=$array['pid']+1;
                        $member_update['userid']=$us['id'];
                        $member_update['ispay']=1;
                        $member_update['nickname']=$us['nickname'];
                        $member_update['ulevel']=$us['ulevel']+1;
                        add_insert_cl("$table",$member_update);
                    }
            /*        $newsss=getMemberbyID($us['id']);
                    if($newsss['futou']==2){
                        $table='member2';
                    }elseif($newsss['futou']==3){
                        $table='member3';
                    }*/
                    $where = 'where userid=' . $us['id'];
                    $member2 = que_select_cl_where($table,$where);
               //     $sql="select * from ".$table." where ispay>0 and pid<".$member2['pid']."  order by pid desc limit 5,1";
                    $pid = $member2['pid'] - 6;
                    $sql = "select * from " . $table . " where ispay>0 and pid=" . $pid;
                    $member2_piduser= getOne($sql);
                    if($member2_piduser){
                        $iul1 = $_ulevel_cl->getulevelbyulevel($member2_piduser['ulevel']);
                        if ($member2_piduser['recount'] >= $iul1['yl4']) {
                            if($member2_piduser['chuju']==0){
                                $b4 = $iul1['yl2'];
                                if ($b4 > 0) {
                                    $beizhu = $b4 . "出局奖";
                                    edit_sql("update member set wlf=wlf+" . $b4 . ",b2=b2+" . $b4 . " where id=" . $member2_piduser['userid']);
                                    edit_sql("update ".$table." set chuju=1 where id=" . $member2_piduser['id']);
                                    $_bonus_cl->bonus_laiyuan($member2_piduser['userid'], $member2_piduser['nickname'], $us['id'], $us['nickname'], 2, $b4, $beizhu);
                                }
                            }
                        }
                    }
                    $ulevel=$us['ulevel']+1;
                    if($_iul['yl6']>0){
                        $yl6=$_iul['yl6'];
                        edit_sql("update member set cfxf=cfxf+".$yl6."  where id=" . $us['id']);
                    }
                    edit_sql("update member set futou=futou+1,ulevel=".$ulevel."  where id=" . $us['id']);
                    $beizhu="升级扣除";
                    $beizhu1="升级赠送积分";
                    $_bonus_cl->bonus_laiyuan($us['id'], $us['nickname'], $us['id'], $us['nickname'], 0, -$_iul['yl5'], $beizhu);
                    $_bonus_cl->bonus_laiyuan($us['id'], $us['nickname'], $us['id'], $us['nickname'], 0, $_iul['yl6'], $beizhu1);
                    $ulevelup['uid']=$_SESSION['ID'];
                    $ulevelup['nickname']=$us['nickname'];
                    $ulevelup['username']=$us['username'];
                    $ulevelup['ylevel']=$us['ulevel'];
                    $ulevelup['uplevel']=$us['ulevel']+1;
                    $ulevelup['bdid']=$us['bdid'];
                    $ulevelup['bdname']=$us['bdname'];
                    $ulevelup['sid']=$us['id'];
                    $ulevelup['snickname']=$us['nickname'];
                    $ulevelup['susername']=$us['username'];
                    $ulevelup['udate']=now();
                    $ulevelup['issh']=0;
                    $ulevelup['cha']=$_iul['yl5'];
                    add_insert_cl('ulevelup',$ulevelup);
             //       echo "<script language=javascript>alert('升级完成.');window.location.href='?'</script>";
                }else{
                    echo "<script language=javascript>alert('余额不足.');window.location.href='?'</script>";
                }
            }else{
                echo "<script language=javascript>alert('推荐人数不足.');window.location.href='?'</script>";
            }
        }

    }

}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="css/table.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="css/style.css" />
    <title>升级</title>
    <script language="javascript">
        <!--
        function checknickname(lx)
        {
            var iframe = document.getElementById("iframe");
            var user =  document.getElementById("nickname");
            iframe.src= "checknickname.php?lx="+lx+"&nickname="+user.value;
        }
        -->
    </script>
</head>
<body>
<iframe name="iframe" id="iframe" width="0" height="0" src="about:blank" style="display:none"></iframe>
<form name="form1" method="post" action="?" onSubmit="return CheckForm();">
    <table  width="100%" cellpadding="3" cellspacing="1" border="0" align="center" class="table1">
        <tr>
            <td align="right">奖金余额:</td>
            <td><?=$us['mey']?></td>
            <td align="right">红包余额:</td>
            <td><?=$us['wlf']?></td>
        </tr>
    </table>
    <table align="center" class="ziti">
        <tr>
            <td><input name="submit" id="submit" type="submit" class="button_green" value="升 级 " onClick="javascript:return confirm('您确认申请升级吗？');"></td>
        </tr>
    </table>
    <br>
    <table width="100%" cellpadding="3" cellspacing="1" border="0" align="center" class="table1">
        <tr>
            <td height="20" colspan="8" align="center"> 申 请 记 录</td>
        </tr>
        <tr>

            <td align="center">升级会员编号</td>
            <td align="center">升级会员姓名</td>
            <td align="center">原级别</td>
            <td align="center">申请级别</td>
            <td align="center">补单金额</td>
            <td align="center">申请时间</td>
            <td align="center">操作人编号</td>
            <td align="center">操作人姓名</td>
            <td style="display:none" align="center">审核状态</td>
        </tr>
        <?php
        $pagesize = 10; //设置每页记录数
        $sql = "SELECT * FROM `ulevelup` WHERE uid=".$_SESSION['ID']." or sid=".$_SESSION['ID']."";
        if($query = mysql_query($sql)){
            $sum = mysql_num_rows($query); //计算总记录数
        }else{
            $sum=0;
        }
        if($sum % $pagesize == 0) //计算总页数
            $total = (int)($sum/$pagesize);
        else
            $total = (int)($sum/$pagesize) + 1;
        if (isset($_GET['page'])) //获得页码
        {
            $p = (int)$_GET['page'];
        }
        else
        {
            $p = 1;
        }
        if ($p>$total){
            $p=$total;
        }
        $start = $pagesize * ($p - 1); //计算起始记录
        $sql = "SELECT * FROM `ulevelup` WHERE uid=".$_SESSION['ID']." or sid=".$_SESSION['ID']." order by id desc limit ".$start.",".$pagesize;
        if($query = mysql_query($sql)){
            while ($row=mysql_fetch_array($query)){
                $yul=ulevel($row['ylevel']);
                $uul=ulevel($row['uplevel']);
                ?>
                <tr>

                    <td align="center"><?=$row['snickname']?></td>
                    <td align="center"><?=$row['susername']?></td>
                    <td align="center"><?=$yul['lvname']?></td>
                    <td align="center"><?=$uul['lvname']?></td>
                    <td align="center"><?=$row['cha']?></td>
                    <td align="center"><?=$row['udate']?></td>
                    <td align="center"><?=$row['nickname']?></td>
                    <td align="center"><?=$row['username']?></td>
                    <td style="display:none"  align="center"><?php if ($row['issh']==1){?>通过审核<?php }else{?> <font color="#FF0000">未审核</font><?php }?></td>
                </tr>
                <?php
            }
        }
        ?>
    </table>
    <table width="100%" border="0" class="ziti">
        <tr>
            <td align="left">&nbsp;</td>
            <td align="right"><?php echo fenye($p,$pagesize,$sum,$total,$cx)?></td>
        </tr>
    </table>
</form>
</body>
</html>